/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.consulta.vista;

import basedatos.OperacionesBD;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import modulos.consulta.modelo.Motivo_Consulta;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class iFrmHCMotivo extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmHCMotivo1
     */
    private ArrayList<Object> listMotivoConsultas;
    private Motivo_Consulta mdfMotivoConsulta;
    private DefaultListModel modeloMotConsul = new DefaultListModel();

    public iFrmHCMotivo() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        listarConsultas();
    }

    private void listarConsultas() {
        listMotivoConsultas = (ArrayList<Object>) OperacionesBD.listar("Motivo_Consulta");
        modeloMotConsul.removeAllElements();
        for (Object obj : listMotivoConsultas) {
            modeloMotConsul.addElement(((Motivo_Consulta) obj).getMotivo());
        }
    }

    private Motivo_Consulta getMotivoConsula(Motivo_Consulta motivo) {
        motivo.setMotivo(txtConsulta.getText().trim());
        return motivo;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtConsulta = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnEditar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listMotConsulas = new javax.swing.JList();
        btnCerrar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setTitle("Motivo de Consulta");
        setToolTipText("");

        jLabel1.setText("Motivo de Consulta");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/icono_editar.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        listMotConsulas.setModel(modeloMotConsul);
        jScrollPane1.setViewportView(listMotConsulas);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                    .addComponent(btnCerrar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCerrar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        if (btnGuardar.getText().equalsIgnoreCase("Guardar")) {
            if (!txtConsulta.getText().isEmpty()) {
                if (!OperacionesBD.existe("Motivo_Consulta", "motivo", txtConsulta.getText())) {
                    if (OperacionesBD.guardar(getMotivoConsula(new Motivo_Consulta()))) {
                        JOptionPane.showMessageDialog(null, "Motivo de consulta guardado correctamente ...", "Guardar", JOptionPane.INFORMATION_MESSAGE);
                        listarConsultas();
                        txtConsulta.setText("");
                    } else {
                        JOptionPane.showMessageDialog(null, "Motivo de consulta no se han podido guardar ...", "Guardar", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "El motivo de consulta ya existe", "Guardar", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Ingrese primero el motivo de consulta", "Guardar", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            if (!txtConsulta.getText().isEmpty()) {
                if (OperacionesBD.modificar(getMotivoConsula(mdfMotivoConsulta))) {
                    JOptionPane.showMessageDialog(null, "Motivo de consulta actualizado correctamente ...", "Actualizar", JOptionPane.INFORMATION_MESSAGE);
                    nuevo();
                    listarConsultas();
                } else {
                    JOptionPane.showMessageDialog(null, "Motivo de consulta no se han podido actualizar ...", "Actualizar", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Ingrese primero el motivo de consulta", "Actualizar", JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnGuardarActionPerformed
    private void nuevo() {
        txtConsulta.setText("");
        btnEditar.setText("Editar");
        btnGuardar.setText("Guardar");
        mdfMotivoConsulta = null;
    }

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        nuevo();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        IFrmHCGeneral.listarMotivoConsultas();
        dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (listMotivoConsultas.size() > 0) {
            if (!listMotConsulas.isSelectionEmpty()) {
                mdfMotivoConsulta = (Motivo_Consulta) listMotivoConsultas.get(listMotConsulas.getSelectedIndex());

                if (JOptionPane.showConfirmDialog(this, "Realmente quiere eliminar el motivo de consulta:\n" + mdfMotivoConsulta.getMotivo(), "Eliminar motivo de consulta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
                    try {
                        if (OperacionesBD.eliminar(mdfMotivoConsulta, mdfMotivoConsulta.getIdMConsulta())) {
                            JOptionPane.showMessageDialog(this, "Motivo de consulta eliminado correctamente ...", "Eliminar motivo de consulta", JOptionPane.INFORMATION_MESSAGE);
                            listarConsultas();
                        } else {
                            JOptionPane.showMessageDialog(this, "El motivo de consulta no ha podido ser eliminado ...", "Eliminar motivo de consulta", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(iFrmHCMotivo.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                }
            } else {
                JOptionPane.showMessageDialog(this, "Seleccione el Motivo de Consulta que desea eliminar ...", "Eliminar motivo de consulta", JOptionPane.WARNING_MESSAGE);

            }
        } else {
            JOptionPane.showMessageDialog(this, "No ha registrado ningun Motivo de Consulta para poder eliminar ...", "Eliminar motivo de consulta", JOptionPane.WARNING_MESSAGE);

        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (btnEditar.getText().equalsIgnoreCase("Editar")) {
            if (listMotivoConsultas.size() > 0) {
                if (!listMotConsulas.isSelectionEmpty()) {

                    mdfMotivoConsulta = (Motivo_Consulta) listMotivoConsultas.get(listMotConsulas.getSelectedIndex());
                    btnGuardar.setText("Actualizar");
                    btnEditar.setText("Cancelar");
                    txtConsulta.setText(mdfMotivoConsulta.getMotivo());
                } else {
                    JOptionPane.showMessageDialog(this, "Seleccione el Motivo de Consulta que desea editar ...", "Editar motivo de consulta", JOptionPane.WARNING_MESSAGE);

                }
            } else {
                JOptionPane.showMessageDialog(this, "No ha registrado ningun Motivo de Consulta para poder editar ...", "Editar motivo de consulta", JOptionPane.WARNING_MESSAGE);

            }
        } else {
            nuevo();
        }


    }//GEN-LAST:event_btnEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listMotConsulas;
    private javax.swing.JTextField txtConsulta;
    // End of variables declaration//GEN-END:variables
}
